package com.tiger.cmpeb.sample5;

import java.rmi.RemoteException;
import java.util.Iterator;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class TestBean01 implements SessionBean {

	private SessionContext sessionContext = null;

	public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException {
		this.sessionContext = sessionContext;
	}

	public void ejbCreate() {

	}

	public void insertData() {
		try {
			InitialContext ctx = new InitialContext();
			StudentHome studentHome = (StudentHome) ctx.lookup("comp/env/ejb/StudentBean");
			SubjectHome subjectHome = (SubjectHome) ctx.lookup("comp/env/ejb/SubjectBean");

//			Subject subject1 = subjectHome.create(new Integer(1), "応用物理");
//			Subject subject2 = subjectHome.create(new Integer(2), "古典");
//			Subject subject3 = subjectHome.create(new Integer(3), "近代史");
//			Subject subject4 = subjectHome.create(new Integer(4), "フランス語");
//			Subject subject5 = subjectHome.create(new Integer(5), "材料力学");
			
			Subject subject1 = subjectHome.create(new Integer(1), "Butsuri");
			Subject subject2 = subjectHome.create(new Integer(2), "Koten");
			Subject subject3 = subjectHome.create(new Integer(3), "History");
			Subject subject4 = subjectHome.create(new Integer(4), "French");
			Subject subject5 = subjectHome.create(new Integer(5), "Chemistory");

			//Student student1 = studentHome.create(new Integer(1), "青木弘");
			Student student1 = studentHome.create(new Integer(1), "aoki hiroshi");
			student1.getSubject().add(subject1);
			student1.getSubject().add(subject2);
			student1.getSubject().add(subject3);

			//Student student2 = studentHome.create(new Integer(2), "加藤清志");
			Student student2 = studentHome.create(new Integer(2), "kato kiyoshi");
			student2.getSubject().add(subject1);
			student2.getSubject().add(subject2);
			student2.getSubject().add(subject4);
			student2.getSubject().add(subject5);

			//Student student3 = studentHome.create(new Integer(3), "佐藤剛志");
			Student student3 = studentHome.create(new Integer(3), "sato tsuyoshi");
			student3.getSubject().add(subject2);
			student3.getSubject().add(subject3);
			student3.getSubject().add(subject5);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {
			e.printStackTrace();
		}
	}

	public void test() {
		try {
			InitialContext ctx = new InitialContext();
			SubjectHome subjectHome = (SubjectHome) ctx.lookup("comp/env/ejb/SubjectBean");

			for (int i = 1; i <= 5; i++) {
				Subject subject = subjectHome.findByPrimaryKey(new Integer(i));
				System.out.println("Subject-Name = " + subject.getName());
				Iterator iterator = subject.getStudent().iterator();
				while (iterator.hasNext()) {
					Student student = (Student) iterator.next();
					System.out.println("-----Student-Name = " + student.getName());
				}
				System.out.println();
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}

	}

	public void ejbActivate() throws EJBException, RemoteException {
	}

	public void ejbPassivate() throws EJBException, RemoteException {
	}

	public void ejbRemove() throws EJBException, RemoteException {
	}

}
